package com.shangxia.dreaming.business;

import com.shangxia.dreaming.common.content.ConditionContent;
import com.shangxia.dreaming.common.util.txtUtil.ReadTXT;
import com.shangxia.dreaming.common.util.txtUtil.WriteTXT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;

/**
 * @ProjectName: dreaming
 * @ClassName: SixJob
 * @Time: 2020/10/30
 * @Author: zhouy
 * @Description: 简单取号  移除或保留某些号码
 * 0-3次错误号
 * @Experience:
 */

@Slf4j
@Component
public class SixJob {

    public void work() throws IOException {
        List<List<Integer>> removeCodeLists = ReadTXT.readList_List_int("移除号码");
        Set<List<Integer>> removeSet_1 = new LinkedHashSet<>();
        Set<List<Integer>> removeSet_2 = new LinkedHashSet<>();
        Set<List<Integer>> removeSet_3 = new LinkedHashSet<>();


        if (removeCodeLists.size() >= 2) {
            for (int i = 0; i < removeCodeLists.size(); i++) {
                List<List<Integer>> removeCodeLists_1 = new ArrayList<>();
                for (int j = 0; j < removeCodeLists.size(); j++) {
                    if (j != i) {
                        removeCodeLists_1.add(removeCodeLists.get(j));
                    }
                }
                removeSet_1.addAll(removeSets(removeCodeLists_1));
            }
        }

        if (removeCodeLists.size() >= 3) {
            for (int i = 0; i < removeCodeLists.size(); i++) {
                for (int j = i + 1; j < removeCodeLists.size(); j++) {
                    List<List<Integer>> removeCodeLists_2 = new ArrayList<>();
                    for (int k = 0; k < removeCodeLists.size(); k++) {
                        if (k != i && j != k) {
                            removeCodeLists_2.add(removeCodeLists.get(k));
                        }
                    }
                    removeSet_2.addAll(removeSets(removeCodeLists_2));
                }
            }
        }
        if (removeCodeLists.size() >= 4) {
            for (int i = 0; i < removeCodeLists.size(); i++) {
                for (int j = i + 1; j < removeCodeLists.size(); j++) {
                    for (int k = j + 1; k < removeCodeLists.size(); k++) {
                        List<List<Integer>> removeCodeLists_3 = new ArrayList<>();
                        for (int m = 0; m < removeCodeLists.size(); m++) {
                            if (m != i && m != j && m != k) {
                                removeCodeLists_3.add(removeCodeLists.get(m));
                            }
                        }
                        removeSet_3.addAll(removeSets(removeCodeLists_3));
                    }

                }
            }
        }


        List<List<Integer>> saveCodeLists = ReadTXT.readList_List_int("保留号码");
        Set<List<Integer>> saveSet_1 = new LinkedHashSet<>();
        Set<List<Integer>> saveSet_2 = new LinkedHashSet<>();
        Set<List<Integer>> saveSet_3 = new LinkedHashSet<>();
        if (saveCodeLists.size() >= 2) {
            for (int i = 0; i < saveCodeLists.size(); i++) {
                List<List<Integer>> saveCodeLists_1 = new ArrayList<>();
                for (int j = 0; j < saveCodeLists.size(); j++) {
                    if (j != i) {
                        saveCodeLists_1.add(saveCodeLists.get(j));
                    }
                }
                saveSet_1.addAll(saveSets(saveCodeLists_1));
            }
        }

        if (saveCodeLists.size() >= 3) {
            for (int i = 0; i < saveCodeLists.size(); i++) {
                for (int j = i + 1; j < saveCodeLists.size(); j++) {
                    List<List<Integer>> saveCodeLists_2 = new ArrayList<>();
                    for (int k = 0; k < saveCodeLists.size(); k++) {
                        if (k != i && j != k) {
                            saveCodeLists_2.add(saveCodeLists.get(k));
                        }
                    }
                    saveSet_2.addAll(saveSets(saveCodeLists_2));
                }
            }
        }
        if (saveCodeLists.size() >= 4) {
            for (int i = 0; i < saveCodeLists.size(); i++) {
                for (int j = i + 1; j < saveCodeLists.size(); j++) {
                    for (int k = j + 1; k < saveCodeLists.size(); k++) {
                        List<List<Integer>> saveCodeLists_3 = new ArrayList<>();
                        for (int m = 0; m < saveCodeLists.size(); m++) {
                            if (m != i && m != j && m != k) {
                                saveCodeLists_3.add(saveCodeLists.get(m));
                            }
                        }
                        saveSet_3.addAll(saveSets(saveCodeLists_3));
                    }

                }
            }
        }


        Set<List<Integer>> list = getSets(removeCodeLists, saveCodeLists);

        if (list.size() > 0) {
            WriteTXT.saveLists(list, "全结果");
        }
        if (removeSet_1.size() > 0) {
            WriteTXT.saveLists(removeSet_1, "移除容错1次结果");
        }
        if (removeSet_2.size() > 0) {
            WriteTXT.saveLists(removeSet_2, "移除容错2次结果");
        }
        if (removeSet_3.size() > 0) {
            WriteTXT.saveLists(removeSet_3, "移除容错3次结果");
        }
        if (saveSet_1.size() > 0) {
            WriteTXT.saveLists(saveSet_1, "保留容错1次结果");
        }
        if (saveSet_2.size() > 0) {
            WriteTXT.saveLists(saveSet_2, "保留容错2次结果");
        }
        if (saveSet_3.size() > 0) {
            WriteTXT.saveLists(saveSet_3, "保留容错3次结果");
        }

    }

    private Set<List<Integer>> getSets(List<List<Integer>> removeCodeLists, List<List<Integer>> saveCodeLists) {
        String[] split = ConditionContent.TYPE2.split("-");

        Set<List<Integer>> list = creatList(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
        Iterator<List<Integer>> iterator = list.iterator();
        BIG_WHILE:
        while (iterator.hasNext()) {
            List<Integer> next = iterator.next();
            if (removeCodeLists.size() > 0) {
                for (List<Integer> removeCodeList : removeCodeLists) {
                    if (next.containsAll(removeCodeList)) {
                        iterator.remove();
                        continue BIG_WHILE;
                    }
                }
            }
            if (saveCodeLists.size() > 0) {
                for (List<Integer> saveCodeList : saveCodeLists) {
                    if (!next.containsAll(saveCodeList)) {
                        iterator.remove();
                        continue BIG_WHILE;
                    }
                }
            }
        }
        return list;
    }

    private Set<List<Integer>> saveSets(List<List<Integer>> saveCodeLists) {
        String[] split = ConditionContent.TYPE2.split("-");

        Set<List<Integer>> list = creatList(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
        Iterator<List<Integer>> iterator = list.iterator();
        BIG_WHILE:
        while (iterator.hasNext()) {
            List<Integer> next = iterator.next();
            if (saveCodeLists.size() > 0) {
                for (List<Integer> saveCodeList : saveCodeLists) {
                    if (!next.containsAll(saveCodeList)) {
                        iterator.remove();
                        continue BIG_WHILE;
                    }
                }
            }
        }
        return list;
    }

    private Set<List<Integer>> removeSets(List<List<Integer>> removeCodeLists) {
        String[] split = ConditionContent.TYPE2.split("-");
        Set<List<Integer>> list = creatList(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
        Iterator<List<Integer>> iterator = list.iterator();
        BIG_WHILE:
        while (iterator.hasNext()) {
            List<Integer> next = iterator.next();
            if (removeCodeLists.size() > 0) {
                for (List<Integer> removeCodeList : removeCodeLists) {
                    if (next.containsAll(removeCodeList)) {
                        iterator.remove();
                        continue BIG_WHILE;
                    }
                }
            }
        }
        return list;
    }

    private Set<List<Integer>> creatList(int maxSum, int sum) {
        Set<List<Integer>> sets = new LinkedHashSet<>();
        for (int i = 1; i < maxSum + 2 - sum; i++) {
            for (int j = i + 1; j < maxSum + 3 - sum; j++) {
                if (sum == 2) {
                    List<Integer> list = new ArrayList<>();
                    list.add(i);
                    list.add(j);
                    sets.add(list);
                }
                for (int k = j + 1; k < maxSum + 4 - sum; k++) {
                    if (sum == 3) {
                        List<Integer> list = new ArrayList<>();
                        list.add(i);
                        list.add(j);
                        list.add(k);
                        sets.add(list);
                    }
                    for (int m = k + 1; m < maxSum + 5 - sum; m++) {
                        if (sum == 4) {
                            List<Integer> list = new ArrayList<>();
                            list.add(i);
                            list.add(j);
                            list.add(k);
                            list.add(m);
                            sets.add(list);
                        }
                        for (int n = m + 1; n < maxSum + 6 - sum; n++) {
                            if (sum == 5) {
                                List<Integer> list = new ArrayList<>();
                                list.add(i);
                                list.add(j);
                                list.add(k);
                                list.add(m);
                                list.add(n);
                                sets.add(list);
                            }
                            for (int s = n + 1; s < maxSum + 7 - sum; s++) {
                                if (sum == 6) {
                                    List<Integer> list = new ArrayList<>();
                                    list.add(i);
                                    list.add(j);
                                    list.add(k);
                                    list.add(m);
                                    list.add(n);
                                    list.add(s);
                                    sets.add(list);
                                }
                            }
                        }
                    }
                }
            }
        }
        return sets;
    }
}
